package com.csun.cmny.provider.controller.admin;

import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.yingshi.YingShiChannelDto;
import com.csun.cmny.provider.model.dto.yingshi.YingShiChannelQueryDto;
import com.csun.cmny.provider.model.vo.yingshi.YingShiChannelVo;
import com.csun.cmny.provider.model.vo.yingshi.YingShiPlayerVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.DmcYingShiChannelService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2021/8/27 16:37
 * @Description: 萤石摄像头通道控制器
 */
@RestController
@RequestMapping(value = "/yingShiChannel", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class DmcYingShiChannelController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private DmcYingShiChannelService dmcYingShiChannelService;

    /**
     * 获取播放地址
     */
    @RequestMapping(value = "/getPlayUrl", method = RequestMethod.POST)
    public Wrapper getPlayUrl(Long id) {

        YingShiPlayerVo yingShiPlayerVo = dmcYingShiChannelService.getPlayUrl(id);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                yingShiPlayerVo);
    }

    /**
     * 查询萤石摄像头通道列表 - 分页
     *
     * @param yingShiChannelQueryDto the ying shi channel query dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/queryListWithPage", method = RequestMethod.POST)
    public Wrapper queryListWithPage(@RequestBody YingShiChannelQueryDto yingShiChannelQueryDto) {

        List<YingShiChannelVo> yingShiChannelVoList = dmcYingShiChannelService.queryListWithPage(yingShiChannelQueryDto);

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null),
                new PageInfo<>(yingShiChannelVoList));
    }

    /**
     * 添加萤石摄像头通道信息
     *
     * @param yingShiChannelDto  the ying shi channel dto
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Wrapper save(@Valid @RequestBody YingShiChannelDto yingShiChannelDto) {

        Integer result = dmcYingShiChannelService.save(yingShiChannelDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 修改萤石摄像头通道信息
     *
     * @param yingShiChannelDto  the ying shi camera dto
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Wrapper edit(@RequestBody YingShiChannelDto yingShiChannelDto) {

        Integer result = dmcYingShiChannelService.edit(yingShiChannelDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }

    /**
     * 根据id删除萤石摄像头通道信息
     *
     * @param id    the id
     *
     * @return  the wrapper.
     */
    @RequestMapping(value = "/deleteById/{id}", method = RequestMethod.POST)
    public Wrapper deleteById(@PathVariable Long id) {

        Integer result = dmcYingShiChannelService.deleteById(id);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE,
                    cmnyMessageSource.getMessage("operation.failed",null));
        }

        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,
                cmnyMessageSource.getMessage("operation.success", null), result);
    }
}
